<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-button type="primary" @click="addDialogVisible = true">添加角色</el-button>
      </el-row>
      <el-table :data="rolesList" border style="width: 100%" stripe row-key="id">
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bdbottom',i==0?'bdtop':'','vcenter']"
              v-for="(iteam,i) in scope.row.children"
              :key="iteam.id"
            >
              <el-col :span="5">
                <el-tag closable @close="removeRolesById(scope.row,iteam.id)">{{iteam.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="19">
                <el-row
                  v-for="(itea,j) in iteam.children"
                  :key="itea.id"
                  :class="[j==0?'':'bdtop','vcenter']"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removeRolesById(scope.row,itea.id)"
                    >{{itea.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      type="warning"
                      closable
                      v-for="(ite,k) in itea.children"
                      :key="ite.id"
                      @close="removeRolesById(scope.row,ite.id)"
                    >{{ite.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>
                      {{scope.row}}
            </pre>-->
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="setRoles(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="分配权限" :visible.sync="setRolesVisible" width="50%" @close="setDialogClose">
      <el-tree
        :data="rolesTree"
        show-checkbox
        node-key="id"
        default-expand-all
        :props="rolesProps"
        :default-checked-keys="defKeys"
        ref="treeRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRolesVisible = false">取 消</el-button>
        <el-button type="primary" @click="trueAndSetRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      rolesList: [],
      setRolesVisible: false,
      rolesTree: [],
      rolesProps: {
        children: "children",
        label: "authName"
      },
      defKeys: [],
      rolesId: ""
    };
  },
  created() {
    this.getRolesList();
  },
  methods: {
    async getRolesList() {
      const { data: res } = await this.$http.get("/roles");
      if (res.meta.status != 200) {
        this.$message.error("获取信息失败");
      } else {
        this.rolesList = res.data;
      }
    },
    async setRoles(role) {
      this.rolesId = role.id;
      const { data: res } = await this.$http.get("rights/tree");
      if (res.meta.status != 200) {
        this.$message.error("获取权限列表失败");
      } else {
        this.rolesTree = res.data;
        this.getDefKeys(role, this.defKeys);
      }
      this.setRolesVisible = true;
    },
    async removeRolesById(info, id) {
      const result = await this.$confirm(
        "此操作将永久删除该权限, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      if (result == "confirm") {
        const { data: res } = await this.$http.delete(
          `roles/${info.id}/rights/${id}`
        );
        if (res.meta.status != 200) {
          this.$message.error("删除失败");
        } else {
          this.$message.success("删除成功");
          info.children = res.data;
        }
      } else {
        this.$message.info("已经取消删除");
      }
    },
    getDefKeys(node, arr) {
      if (node.children) {
        node.children.forEach(iteam => {
          this.getDefKeys(iteam, arr);
        });
      } else {
        return arr.push(node.id);
      }
    },
    setDialogClose() {
      this.defKeys = [];
    },
    async trueAndSetRights() {
      const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ];
      const idString = keys.join(",");
      const { data: res } = await this.$http.post(`roles/${this.rolesId}/rights`, {
        rids: idString
      });
      if (res.meta.status != 200) {
        return this.$message.error("更新权限失败");
      }
      this.getRolesList();
      this.setRolesVisible=false
    }
  }
};
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>